package com.fscp.seckill.mapper;

import com.fscp.commons.model.pojo.SeckillVoucher;
import org.apache.ibatis.annotations.*;

/**
 * 秒杀代金券 Mapper
 */
public interface SeckillVoucherMapper {

    /**
     * 新增秒杀活动
     * mybatis应用——@Options
     * useGeneratedKeys 是否返回生成的主键
     * keyProperty 传入对象中的对象名
     * keyColumn 数据库中的字段名
     *
     * @param seckillVoucher
     * @return
     */
    @Insert("insert into t_seckill_voucher (fk_voucher_id, amount, start_time, end_time, is_valid, create_date, update_date) " +
            " values (#{fkVoucherId}, #{amount}, #{startTime}, #{endTime}, 1, now(), now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int save(SeckillVoucher seckillVoucher);

    // 根据代金券 ID 查询该代金券是否参与抢购活动
    @Select("select id, fk_voucher_id, amount, start_time, end_time, is_valid " +
            " from t_seckill_voucher where fk_voucher_id = #{voucherId}")
    SeckillVoucher selectSeckillVoucher(Integer voucherId);

    // 减库存
    @Update("update t_seckill_voucher set amount = amount - 1 " +
            "where id = #{seckillId}")
    int stockDecrease(@Param("seckillId") int seckillId);

}
